<script setup>
    import orderApi from "@/api/order/order.js";
    import userApi from "@/api/user/user.js";
    import {ref, reactive} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";

    //表格数据
    const list = ref([])
    const total = ref(0)
    //分页信息和搜索条件
    const orderInfoQuery = reactive({
        page: 1,
        limit: 10
    })

    const statusList = [
        {
            id: 0,
            name: '待付款'
        },
        {
            id: 1,
            name: '待发货'
        },
        {
            id: 2,
            name: '待收货'
        },
        {
            id: 3,
            name: '已完成'
        },
        {
            id: 4,
            name: '已撤销'
        }
    ]

    //搜素全部的用户
    const userList = ref([])
    const loadUsers = () => {
        userApi.selectAll().then(result => {
            userList.value = result.data
        })
    }
    loadUsers()

    const loadData = () => {
        orderApi.selectByPage(orderInfoQuery).then(result => {
            list.value = result.data.list
            total.value = result.data.total
        })
    }
    loadData()

    const onSearch = () => {
        orderInfoQuery.page = 1;
        loadData();
    }

    const dialogFormVisible = ref(false);
    const orderItemList = ref([])
    const showDetailDialog = (id) => {
        dialogFormVisible.value = true;
        orderItemList.value = [];
        orderApi.selectItem(id).then(result => {
            if (result.code === 0) {
                orderItemList.value = result.data.list
            }
        })
    }

    const shipment = (id) => {
        orderApi.shipment(id).then(result => {
            if (result.code === 0) {
                ElMessage({message: result.msg, type: 'success',})
                location.reload();
            }
        })
    }

    const cancel = (id) => {
        orderApi.cancel(id).then(result => {
            if (result.code === 0) {
                ElMessage({message: result.msg, type: 'success',})
                location.reload();
            }
        })
    }
</script>

<template>
    <el-card class="">
        <template #header>订单列表</template>

        <el-form :inline="true">
            <el-form-item label="用户姓名"  style="width: 200px" :label-width="70">
                <el-select v-model="orderInfoQuery.userId" clearable placeholder="请选择用户">
                    <el-option
                        v-for="user in userList"
                        :key="user.id"
                        :label="user.name"
                        :value="user.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="订单状态"  style="width: 200px" :label-width="70">
                <el-select v-model="orderInfoQuery.orderStatus" clearable placeholder="请选择状态">
                    <el-option
                        v-for="status in statusList"
                        :key="status.id"
                        :label="status.name"
                        :value="status.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                    v-model="orderInfoQuery.beginCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="开始时间"
                />
                <el-date-picker
                    v-model="orderInfoQuery.endCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="结束时间"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="list" style="width: 100%" ref="multipleTableRef">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="ID" width="50" />
            <el-table-column prop="userName" label="用户姓名"/>
            <el-table-column prop="orderNo" label="订单号"/>
            <el-table-column prop="totalAmount" label="订单总额"/>
            <el-table-column prop="orderStatus" label="订单状态">
                <template #default="scope">
                    <p v-if="scope.row.orderStatus===0">待付款</p>
                    <p v-if="scope.row.orderStatus===1">待发货</p>
                    <p v-if="scope.row.orderStatus===2">待收货</p>
                    <p v-if="scope.row.orderStatus===3">已完成</p>
                    <p v-if="scope.row.orderStatus===4">已撤销</p>
                </template>
            </el-table-column>
            <el-table-column prop="cancelReason" label="撤销原因"/>
            <el-table-column prop="createTime" label="创建时间"/>
            <el-table-column prop="updateTime" label="更新时间"/>
            <el-table-column fixed="right" label="操作" align="center" min-width="200">
                <template #default="{ row }">
                    <el-button type="primary" @click="showDetailDialog(row.id)">订单详情</el-button>
                    <el-button :disabled="row.orderStatus!==1" type="success" @click="shipment(row.id)">确认发货</el-button>
                    <el-button :disabled="row.orderStatus!==0 && row.orderStatus!==1" type="danger" @click="cancel(row.id)">取消订单</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="orderInfoQuery.page"
            v-model:page-size="orderInfoQuery.limit"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @change="loadData"
            style="margin-top: 20px; justify-content: flex-end"/>

        <!--订单详情的弹出框-->
        <el-dialog v-model="dialogFormVisible" title="订单详情" width="70%" :lock-scroll="false">
            <el-table :data="orderItemList" style="width: 100%" ref="multipleTableRef">
                <el-table-column type="selection" width="55" />
                <el-table-column fixed prop="id" label="ID" width="50" />
                <el-table-column prop="productName" label="商品名"/>
                <el-table-column prop="productImage" label="图片">
                    <template #default="scope">
                        <img :src="scope.row.productImage" style="max-height: 40px;max-width: 120px" />
                    </template>
                </el-table-column>
                <el-table-column prop="unitPrice" label="单价"/>
                <el-table-column prop="quantity" label="数量"/>
                <el-table-column prop="createTime" label="创建时间"/>
                <el-table-column prop="updateTime" label="更新时间"/>
            </el-table>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">关闭</el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>

<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }
</style>